# Maintainer: Fabien Chouteau <chouteau@adacore.com>

_realname=stlink
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.6.1
pkgrel=1
pkgdesc='Firmware programmer for STM32 STLINK v1/v2 protocol'
arch=('any')
url='https://github.com/stlink-org/stlink'
license=(BSD)
depends=("${MINGW_PACKAGE_PREFIX}-libusb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gcc")
source=(${_realname}-${pkgver}.zip::https://github.com/stlink-org/stlink/archive/v$pkgver.zip)
sha256sums=('859af69fae8dc0751178c93a0558c55a99f4c57c17287ca32c8344021d56f170')

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install
}
